import com.LearnMybatisPlusApplication;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoming.dao.UserDao;
import com.xiaoming.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoMing
 * @since 2023/3/13 0:08
 */
@SpringBootTest(classes = LearnMybatisPlusApplication.class)
public class testMybatisplus {
    @Resource
    private UserDao userDao;
    @Test
    public void test01(){
        System.out.println("hello testMybatisplus");
    }

    @Test
    void contextLoads() {
// 查询name不为空的用户，并且邮箱不为空的用户，年龄大于等于12
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper
                .isNotNull("name")
                .isNotNull("email")
                .ge("age",12);
        userDao.selectList(wrapper).forEach(System.out::println); // 和我们刚才学习的map对比一下
    }

    @Test
    void testAllEq(){
        Map<String, Object> params = new HashMap<>();
        params.put("name", "xiaoming1");
        params.put("age", "181");
        params.put("version", null);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        map 所有非空属性等于 =
            //形参:
            //condition – 执行条件
            //params – map 类型的参数, key 是字段名, value 是字段值
            //null2IsNull – 是否参数为 null 自动执行 isNull 方法, false 则忽略这个字段\
        wrapper.allEq((k, v) -> (k.equals("age") || k.equals("id")), params);

        List<User> users = this.userDao.selectList(wrapper);
        //SELECT
        // id,name,age,email,create_time,update_time,version,deleted
        // FROM user WHERE deleted=0 AND (age = '181')
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    void testActiveRecord(){
        User user = new User();

        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.likeRight("name","白");//name LIKE '白'%(String) 以白开始
        wrapper.likeLeft("name","白");//name LIKE %'白'(String)以白结尾
        List<User> result = user.selectList(wrapper);
        result.forEach(System.out::println);
//        System.out.println("result -》" + result);
    }

}
